<template>
  <div class="exchange-management-page">
    <!-- 搜索区域 -->
    <div class="search-section">
      <el-form :inline="true" :model="searchForm" ref="searchForm">
        <el-form-item label="搜索">
          <el-input
            v-model="searchForm.searchKey"
            placeholder="名称、编号、关键字等"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 数据表格 -->
    <el-table
      :data="tableData"
      stripe
      v-loading="tableLoading"
      element-loading-text="正在加载数据..."
    >
      <el-table-column prop="id" label="序号" width="80"></el-table-column>
      <el-table-column prop="name" label="交易所名称" width="180"></el-table-column>
      <el-table-column prop="city" label="城市" width="120"></el-table-column>
      <el-table-column prop="businessScope" label="业务范围" width="720">
        <template slot-scope="scope">
          {{ scope.row.businessScope }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="text" @click="handleView(scope.row)">查看</el-button>
          <el-button type="text" @click="handleOpenAccount(scope.row)">开户</el-button>
          <el-button type="text" @click="handleTrade(scope.row)">交易</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "ExchangeManagementPage",
  data() {
    return {
      // 搜索表单
      searchForm: {
        searchKey: "",
      },
      // 表格数据
      tableData: [],
      tableLoading: false,
      totalCount: 0,
      currentPage: 1,
      pageSize: 10,
    };
  },
  created() {
    this.fetchTableData();
  },
  methods: {
    // 获取表格数据
    async fetchTableData() {
      this.tableLoading = true;
      try {
        // 模拟接口请求，实际替换为真实接口及参数
        const res = await this.$request.post("/assets/carbonExchange/getListByTenantB", {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          ...this.searchForm,
        });
        if (res.code === 200) {
            console.log("ressdd",res)
          this.tableData = res.data.list;
          this.totalCount = res.data.total;
        }
      } catch (error) {
        console.error("获取数据失败:", error);
        this.$message.error("网络错误，请重试");
      } finally {
        this.tableLoading = false;
      }
    },
    // 搜索
    handleSearch() {
      this.currentPage = 1;
      this.fetchTableData();
    },
    // 查看操作 - 跳转到百度搜索该交易所
    handleView(row) {
          const button = document.activeElement;

      // 构建百度搜索URL，将交易所名称作为搜索关键词
      const searchUrl = `https://baike.baidu.com/item/${encodeURIComponent(row.name)}`;
      
      // 在新窗口打开百度搜索页面
      window.open(searchUrl, '_blank');
       if (button && button.blur) {
    // 延迟执行，确保跳转逻辑完成后再失焦
    setTimeout(() => {
      button.blur();
      // 额外措施：将焦点移到文档其他地方
      document.body.focus();
    }, 100);
  }
    },
    // 开户操作
    handleOpenAccount(row) {
      this.$router.push('/account/info') 
      // 可补充调用开户接口等逻辑
    },
     handleTrade(row) {
      // 记录当前点击的按钮
      const button = document.activeElement;
      
      // 使用数据库返回的website字段作为官网地址
      if (row.website) {
        // 确保链接以http/https开头
        const url = row.website.startsWith('http') 
          ? row.website 
          : `https://${row.website}`;
        
        // 在新窗口打开交易所官网
        window.open(url, '_blank');
      } else {
        this.$message.warning(`交易所 ${row.name} 未配置官网地址`);
      }
      
      // 移除按钮焦点，解决按钮"一直被按着"的问题
      if (button && button.blur) {
        setTimeout(() => {
          button.blur();
          document.body.focus();
        }, 100);
      }
    },
    // 每页条数改变
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchTableData();
    },
    // 当前页改变
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchTableData();
    },
  },
};
</script>

<style scoped>
.exchange-management-page {
  padding: 20px;
  background-color: #fff;
  min-height: calc(100vh - 40px);
}

.search-section {
  margin-bottom: 20px;
  padding: 15px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}

::v-deep .el-table th {
  background-color: #f5f7fa;
  font-weight: 500;
}

::v-deep .el-table td,
::v-deep .el-table th {
  text-align: center;
}

/* 响应式 */
@media screen and (max-width: 1200px) {
  .search-section {
    overflow-x: auto;
    padding-bottom: 15px;
  }
}
</style>
